package mysql

import (
	"gorm.io/gorm"
	"short-vid-joy/shortrpc/shortrpc"
)

type Comment struct {
	gorm.Model
	UserId        int64  `gorm:"type:int(11);not null;comment:'用户id'"`
	VideoId       int64  `gorm:"type:int(11);not null;comment:'视频id'"`
	Content       string `gorm:"type:varchar(500);not null;comment:'评论内容'"`
	IsAnonymous   int64  `gorm:"type:tinyint(11);default:0;comment:'是否匿名,0-非匿名，1-匿名'"`
	commentStatus int64  `gorm:"type:tinyint(11);default:0;comment:'评论状态,0-有效，1-违规（含敏感词）'"`
	ReplyId       int64  `gorm:"type:int(11);not null;comment:'回复id'"`
}

func (c *Comment) CreateComment(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Comment) FindCommentById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).Error
}

func (c *Comment) DelComment(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).Delete(c).Error
}

func CommentList(db *gorm.DB, in *shortrpc.CommentListRequest, list []*shortrpc.CommentList) []*shortrpc.CommentList {
	db.Debug().Model(&Comment{}).
		Select("comments.id as comment_id,nick_name,comments.content,comments.created_at").
		Joins("left join users on users.id = comments.user_id").
		Where("comments.user_id= ? and comments.video_id = ?", in.UserId, in.VideoId).
		Scopes(Paginate(int(in.Page), int(in.PageSize))).Find(&list)
	return list
}
